package com.example.apprendrechinois.cours;

import java.util.ArrayList;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.apprendrechinois.R;

public class AMotCleAdapter extends ArrayAdapter<ACoursText>{

	private ArrayList<ACoursText> allContext;
	private MediaPlayer mp;
	private int coursnumber;
	private int pos;
	
	public void setCoursNumber(int n){
		this.coursnumber = n;
	}
	
	public AMotCleAdapter(Context context, ArrayList<ACoursText> acourstext){
		super(context, R.layout.a_motcle, acourstext);	
	}

	
	public View getView (int position, View convertView, ViewGroup parent){
		ACoursText item = this.getItem(position);
		pos = position;
		System.out.println(item.getTextCH()+item.getTextFR()+item.getTextSonFile());
		if(convertView != null){
			fillView(convertView, item);
			return convertView;
		}
		else {
		
			View view = LayoutInflater.from(this.getContext()).inflate(R.layout.a_motcle, null);
			fillView(view, item);
			return view;
		}	
	}
	
	private void fillView(View view, ACoursText item){
		try{
			if(coursnumber==0){//cours1
				if(pos==0){
					mp = MediaPlayer.create(getContext(), R.raw.mot1_1);
				}else if(pos==0){
					mp = MediaPlayer.create(getContext(), R.raw.mot1_1);
				}else{
					mp = MediaPlayer.create(getContext(), R.raw.mot1_1);
				}
			}else if(coursnumber==1){
				
			}else{
				
			}
		
		}catch(Exception e){
			e.printStackTrace();
		}
		Button bSon =(Button)view.findViewById(R.id.Prononciation);
	    bSon.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					try{
						if(mp==null) System.out.println("File MP3 don't exist!");
						mp.start();
					}catch(Exception e){
						e.printStackTrace();
					}
					
				}
	          });
    
		TextView tch = (TextView)view.findViewById(R.id.textChinois);
		tch.setText(item.getTextCH());
	    
		TextView tfr = (TextView)view.findViewById(R.id.textFrancais);
		tfr.setText(item.getTextFR());
		
	}
}
